//
//  LeftSortViewController.m
//  WWXELuShang
//
//  Created by 王涛 on 15/12/23.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "LeftSortViewController.h"
#import "AppDelegate.h"
#import "PersonTableViewCell.h"
#import "ServeViewController.h" //服务条款
#import "WXMWebViewController.h" //违章查询
#import "CouponViewController.h" //优惠券
#import "MyAdvViewController.h" //关于我们
#import "SetUpCenterTableViewController.h" //设置中心

@interface LeftSortViewController ()<UITableViewDataSource,UITableViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    UIView * _headView;
}
@property (nonatomic, strong) UIImageView *peosonView;
@end

@implementation LeftSortViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    [NSThread sleepForTimeInterval:1];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PersonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonCell" forIndexPath:indexPath];
    NSMutableArray *imageArray = [NSMutableArray array];
    for (int i = 1; i < 6; i ++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"person%d.png",i]];
        [imageArray addObject:image];
    }
    NSArray *nameArray = @[@"违章查询",@"优惠券",@"服务条款",@"关于我们",@"设置中心"];
    //违章查询是查询驾驶证是否有扣分情况，我的收藏是收藏二手车、行车咨询等相关信息，优惠券目前只放空的优惠券，为了后续工作的开发，服务条款写服务的一些协议等等，关于我们内部写意见反馈以及开发者信息，设置中心设置推送设置，可以的话可以设置推送设置以及推送接收时间，清除缓存，检测更新，意见反馈，推送设置等等
    cell.personView.image = imageArray[indexPath.row];
    cell.nameLabel.text = nameArray[indexPath.row];
    cell.backgroundColor= kLeft;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.leftSlideVC closeLeftView];//关闭左侧抽屉
    //对应单元格显示对应页面
    
    //违章查询
    if (indexPath.row == 0) {
        [tempAppDelegate.leftSlideVC closeLeftView];
        [WXMWebViewController showWithController:tempAppDelegate.mainNC.viewControllers[0] withUrlStr:@"http://m.weizhang8.cn/" withTitle:@"违章查询"];
    }
    
    //优惠劵
    if (indexPath.row == 1) {
        CouponViewController *couponVC = [[CouponViewController alloc] init];
        [tempAppDelegate.mainNC pushViewController:couponVC animated:YES];
    }
    
    
    //服务条款
    if (indexPath.row == 2) {
        [tempAppDelegate.leftSlideVC closeLeftView]; //关闭左抽屉
        
        ServeViewController *serveVC = [[ServeViewController alloc]init];
        serveVC.view.backgroundColor = kBackGroundColor;
        serveVC.title = @"服务条款";
        //        UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:serveVC];
        
        //        [tempAppDelegate.mainNC presentViewController:navC animated:YES completion:nil];
        [tempAppDelegate.mainNC pushViewController:serveVC animated:YES];
    }
    
    //关于我们
    if (indexPath.row == 3) {
        [tempAppDelegate.leftSlideVC closeLeftView]; //关闭左抽屉
        MyAdvViewController *myAdv = [[MyAdvViewController alloc]init];
        myAdv.title = @"关于我们";
        [tempAppDelegate.mainNC pushViewController:myAdv animated:YES];
        
        
        
        
    }
    
    //设置中心：推送设置，意见反馈，清除缓存，检测更新，
    if (indexPath.row == 4) {
        NSLog(@"设置中心");
        SetUpCenterTableViewController *setUpCenterVC = [[SetUpCenterTableViewController alloc]init];
        setUpCenterVC.title = @"设置中心";
        [tempAppDelegate.mainNC pushViewController:setUpCenterVC animated:YES];
        
        
        
    }
    
    
    
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kHeight / 12;
}
//设置头视图
- (void)setupUI{
    self.view.backgroundColor = kLeft;
    UITableView * tableView = [[UITableView alloc]init];
    self.tableView = tableView;
    tableView.frame = self.view.bounds;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    
    _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, kHeight / 3.5)];
    _headView.backgroundColor = kLeft;
    self.tableView.tableHeaderView = _headView;
    
    self.peosonView = [[UIImageView alloc] initWithFrame:(CGRectMake(kWidth / 18.75, kHeight / 3.5 / 2 - kHeight / 8 / 2, kHeight / 8, kHeight / 8))];
    self.peosonView.image = [UIImage imageNamed:@"setting0@3x"];
    [self setImageLayer:self.peosonView];
    [self.view addSubview:self.peosonView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addPicture)];
    self.peosonView.userInteractionEnabled = YES;//打开用户交互
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"aa.png"]];
    UIImage *image = [UIImage imageWithContentsOfFile:filePath];
    if (image != nil) {
        self.peosonView.image = image;
    }
    [self.peosonView addGestureRecognizer:tap];
    
    UILabel *label = [[UILabel alloc] initWithFrame:(CGRectMake(kWidth / 18.75 + kHeight / 6.5, kHeight / 3.5 / 2 - 20, kWidth / 3, 40))];
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentLeft;
    label.text = @"个人中心  >";
    label.font = [UIFont boldSystemFontOfSize:18];
    [self.view addSubview:label];
    
    self.tableView.backgroundColor = kLeft;
    //    self.tableView.scrollEnabled = NO;//是否允许滑动
    self.tableView.separatorColor = kLeft;//分割线颜色
    [self.tableView registerClass:[PersonTableViewCell class] forCellReuseIdentifier:@"PersonCell"];
}

- (void)setImageLayer:(UIImageView *)imageView {
    imageView.layer.borderColor = [kBarColor CGColor];
    imageView.layer.borderWidth = 3;
    imageView.layer.cornerRadius = kHeight / 8 / 2;
    imageView.layer.masksToBounds = YES;
}
//点击图片时，模态显示图片
- (void)addPicture {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:picker animated:YES completion:nil];
}
//选择图片后执行
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    self.peosonView.image = image;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"aa.png"]];//保存文件的名称
    [UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
    [picker dismissViewControllerAnimated:YES completion:nil];
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
